<?php
/**
 * Functions to manipulate strings
 */
require_once APP_CLASS . '/MyString.php';

require_once(dirname(__FILE__) . "/PluginInterface.php");

class PluginManager 
{
    public function __construct()
    {
    }
    
    public function getPluginsActive()
    {
        $answer = array();     
        
        if (file_exists(APP_USER . "/PluginList.conf")) {
            
            $contents = file(APP_USER . "/PluginList.conf");
            foreach($contents as $plugin) {
                $plugin = MyString::removeLast($plugin);
                if (file_exists(APP_PLUGIN . "/$plugin/$plugin.php")) {
                    $answer[] = $plugin;
                }
            }
        }
        return($answer);
    }
    
    public function loadActive()
    {
        $app = Application::singleton();
        $pluginList = $this->getPluginsActive();
        foreach($pluginList as $pluginName) {
            $code = "require_once(\"" . APP_PLUGIN . "/$pluginName/$pluginName.php\");\n" .
                    "\$plugin = new $pluginName" . "Plugin();\n" .
                    "\$app->plugins[\"$pluginName\"] = \$plugin;";
            eval($code);
        }
    }
    
    public function loadPlugin($name)
    {
        $app = Applicattion::singleton();
        $code = "\$plugin = new $name();\n" . 
                "\$app->plugins[\"$name\"] = $plugin;";
        eval($code);
    }
    
    
    
}
